home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 …ember: Reference Library / Dev.CD Dec 96 RL / Dev.CD Dec 96 RL.toast / Technical Documentation / develop / develop Issue 27 / develop Issue 27 code / Internet Config Assistant / toolkit / CRect.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-08  |  5.0 KB  |  267 lines  |  [TEXT/CWIE]

  1. #ifndef    __CRECT__
  2. #define    __CRECT__
  3.  
  4. #include "CPoint.h"
  5.  
  6. #include <QuickDraw.h>
  7.  
  8. class CRect 
  9. {
  10. public:
  11.         // constructors
  12.         CRect();
  13.         CRect(const CRect &);
  14.         CRect(GraphicalUnit l, GraphicalUnit t, GraphicalUnit r, GraphicalUnit b);
  15.         CRect(CPoint leftTop, CPoint rightBottom);
  16.         CRect(const Rect& r);
  17.         
  18.         // destructor
  19.         inline ~CRect() {};
  20.  
  21.         // assignment
  22.         inline CRect& operator=(const CRect &from);
  23.  
  24.         // conversion
  25.         inline operator Rect*();
  26.         inline operator const Rect*() const;
  27.         inline operator Rect() const;
  28.  
  29.         // accessors
  30.         inline GraphicalUnit Width() const;
  31.         inline GraphicalUnit Height() const;
  32.         inline GraphicalUnit Left() const;
  33.         inline GraphicalUnit Top() const;
  34.         inline GraphicalUnit Right() const;
  35.         inline GraphicalUnit Bottom() const;
  36.  
  37.         inline CPoint LeftTop() const;
  38.         inline CPoint RightBottom() const;
  39.         inline CPoint LeftBottom() const;
  40.         inline CPoint RightTop() const;
  41.  
  42.         // mutators
  43.         inline void Set(GraphicalUnit l, GraphicalUnit t, GraphicalUnit r, GraphicalUnit b);
  44.         inline void SetWidth(GraphicalUnit width);
  45.         inline void SetHeight(GraphicalUnit height);
  46.         inline void SetTop(GraphicalUnit top);
  47.         inline void SetBottom(GraphicalUnit bottom);
  48.         inline void SetLeft(GraphicalUnit left);
  49.         inline void SetRight(GraphicalUnit right);
  50.  
  51.         void SetLeftTop(const CPoint);
  52.         void SetRightBottom(const CPoint);
  53.         void SetLeftBottom(const CPoint);
  54.         void SetRightTop(const CPoint);
  55.  
  56.         // transformation
  57.  
  58.         void InsetBy(CPoint);
  59.         inline void InsetBy(GraphicalUnit inset);
  60.         void InsetBy(GraphicalUnit dx, GraphicalUnit dy);
  61.         void OffsetBy(CPoint);
  62.         void OffsetBy(GraphicalUnit dx, GraphicalUnit dy);
  63.         void OffsetTo(CPoint);
  64.         void OffsetTo(GraphicalUnit x, GraphicalUnit y);
  65.  
  66.         // comparison
  67.         Boolean operator==(CRect) const;
  68.         Boolean operator!=(CRect) const;
  69.  
  70.         // intersection and union
  71.         CRect operator&(CRect) const;
  72.         CRect operator|(CRect) const;
  73.  
  74.         // utilities
  75.         Boolean Intersects(CRect r) const;
  76.         inline Boolean IsValid() const;
  77.         inline Boolean IsEmpty() const;
  78.         Boolean Contains(CPoint) const;
  79.         Boolean Contains(CRect) const;
  80.         void Center(CRect outsideRect, Boolean scaleIfNeeded = true);
  81.  
  82. private:
  83.         Rect    fRect;
  84. };
  85.  
  86. //------------------------------------------------------------------------------
  87.  
  88. inline CRect::CRect()
  89. {
  90.     // make initial rect invalid
  91.     fRect.top = fRect.left = 0;
  92.     fRect.bottom = fRect.right = -1;
  93. }
  94.  
  95. inline CRect::CRect(const CRect &r)
  96. {
  97.     fRect.left = r.Left();
  98.     fRect.top = r.Top();
  99.     fRect.right = r.Right();
  100.     fRect.bottom = r.Bottom();
  101. }
  102.  
  103. inline CRect::CRect(GraphicalUnit l, GraphicalUnit t, GraphicalUnit r, GraphicalUnit b)
  104. {
  105.     fRect.left = l;
  106.     fRect.top = t;
  107.     fRect.right = r;
  108.     fRect.bottom = b;
  109. }
  110.  
  111. inline CRect::CRect(CPoint leftTop, CPoint rightBottom)
  112. {
  113.     fRect.left = leftTop.X();
  114.     fRect.top = leftTop.Y();
  115.     fRect.right = rightBottom.X();
  116.     fRect.bottom = rightBottom.Y();
  117. }
  118.  
  119. inline CRect::CRect(const Rect& r)
  120. {
  121.     fRect.left = r.left;
  122.     fRect.top = r.top;
  123.     fRect.right = r.right;
  124.     fRect.bottom = r.bottom;
  125. }
  126.  
  127. inline CRect &CRect::operator=(const CRect& from)
  128. {
  129.     // don't need to worry about "this==from"
  130.     fRect.left = from.fRect.left;
  131.     fRect.top = from.fRect.top;
  132.     fRect.right = from.fRect.right;
  133.     fRect.bottom = from.fRect.bottom;
  134.     return *this;
  135. }
  136.  
  137. inline void CRect::Set(GraphicalUnit l, GraphicalUnit t, GraphicalUnit r, GraphicalUnit b)
  138. {
  139.     fRect.left = l;
  140.     fRect.top = t;
  141.     fRect.right = r;
  142.     fRect.bottom = b;
  143. }
  144.  
  145. inline CRect::operator Rect*()
  146. {
  147.     return &fRect;
  148. }
  149.  
  150. inline CRect::operator const Rect*() const
  151. {
  152.     return (Rect* const)&fRect;
  153. }
  154.  
  155. inline CRect::operator Rect() const
  156. {
  157.     return fRect;
  158. }
  159.  
  160. inline GraphicalUnit CRect::Width() const
  161. {
  162.     return (Right() - Left());
  163. }
  164.  
  165. inline GraphicalUnit CRect::Height() const
  166. {
  167.     return (Bottom() - Top());
  168. }
  169.  
  170. inline GraphicalUnit CRect::Left() const
  171. {
  172.     return fRect.left;
  173. }
  174.  
  175. inline GraphicalUnit CRect::Top() const
  176. {
  177.     return fRect.top;
  178. }
  179.  
  180.  
  181. inline GraphicalUnit CRect::Right() const
  182. {
  183.     return fRect.right;
  184. }
  185.  
  186.  
  187. inline GraphicalUnit CRect::Bottom() const
  188. {
  189.     return fRect.bottom;
  190. }
  191.  
  192.  
  193. inline CPoint CRect::LeftTop() const
  194. {
  195.     return(CPoint(Left(), Top()));
  196. }
  197.  
  198. inline CPoint CRect::RightBottom() const
  199. {
  200.     return(CPoint(Right(), Bottom()));
  201. }
  202.  
  203. inline CPoint CRect::LeftBottom() const
  204. {
  205.     return(CPoint(Left(), Bottom()));
  206. }
  207.  
  208. inline CPoint CRect::RightTop() const
  209. {
  210.     return(CPoint(Right(), Top()));
  211. }
  212.  
  213. inline void CRect::SetWidth(GraphicalUnit w)
  214. {
  215.     fRect.right = Left() + w;
  216. }
  217.  
  218.  
  219. inline void CRect::SetHeight(GraphicalUnit h)
  220. {
  221.     fRect.bottom = Top() + h;
  222. }
  223.  
  224. inline void CRect::SetTop(GraphicalUnit top)
  225. {
  226.     fRect.top = top;
  227. }
  228.  
  229. inline void CRect::SetBottom(GraphicalUnit bottom)
  230. {
  231.     fRect.bottom = bottom;
  232. }
  233.  
  234. inline void CRect::SetLeft(GraphicalUnit left)
  235. {
  236.     fRect.left = left;
  237. }
  238.  
  239. inline void CRect::SetRight(GraphicalUnit right)
  240. {
  241.     fRect.right = right;
  242. }
  243.  
  244. inline void CRect::InsetBy(GraphicalUnit inset)
  245. {
  246.     InsetBy(inset, inset);
  247. }
  248.  
  249.  
  250. inline Boolean CRect::IsValid() const
  251. {
  252.     if (Left() <= Right() && Top() <= Bottom())
  253.         return true;
  254.     else
  255.         return false;
  256. }
  257.  
  258. inline Boolean CRect::IsEmpty() const
  259. {
  260.     if (Left() == Right() && Top() == Bottom())
  261.         return true;
  262.     else
  263.         return false;
  264. }
  265.  
  266. #endif
  267.